package com.zhuanzhuan.hero.user.init;

import com.bj58.zhuanzhuan.radar.util.RadarUtils;
import com.zhuanzhuan.hunter.mq.consumer.HunterConsumerStarter;
import com.zhuanzhuan.hunter.mq.producer.HunterProducerStarter;
import com.zhuanzhuan.youpin.log.model.LogInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MQInit implements ApplicationListener<ContextRefreshedEvent> {

    private static final LogInfo LOG_INFO = new LogInfo(RadarUtils.getTraceId(), "cmd=ProjectInit");

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        try {
            log.info("mq工具初始化 start logStr={}", LOG_INFO.getLogStr());
            log.info("开始初始化 producers logStr={}", LOG_INFO.getLogStr());
            HunterProducerStarter.startProducers(LOG_INFO.getLogStr(), event.getApplicationContext());
            log.info("开始初始化 consumers logStr={}", LOG_INFO.getLogStr());
            HunterConsumerStarter.startConsumers(LOG_INFO.getLogStr(), event.getApplicationContext());
            log.info("mq工具初始化 end logStr={}", LOG_INFO.getLogStr());
            log.info("onApplicationEvent end logStr={}", LOG_INFO.getLogStr());

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
